iT邦幫忙

2022 iThome 鐵人賽

DAY 14
0

未完待續...

def relative_strength_idx(df, n=14):
    close = df['close']
    delta = close.diff()
    delta = delta[1:]
    pricesUp = delta.copy()
    pricesDown = delta.copy()
    pricesUp[pricesUp < 0] = 0
    pricesDown[pricesDown > 0] = 0
    rollUp = pricesUp.rolling(n).mean()
    rollDown = pricesDown.abs().rolling(n).mean()
    rs = rollUp / rollDown
    rsi = 100.0 - (100.0 / (1.0 + rs))
    return rsi

# SMA
df['EMA_3'] = df['close'].ewm(3).mean().shift()
df['EMA_7'] = df['close'].ewm(7).mean().shift()
df['EMA_30'] = df['close'].ewm(30).mean().shift()

# EMA
df['SMA_3'] = df['close'].rolling(3).mean().shift()
df['SMA_7'] = df['close'].rolling(7).mean().shift()
df['SMA_30'] = df['close'].rolling(30).mean().shift()

# RSI
df['RSI'] = relative_strength_idx(df).fillna(0)

# MACD
EMA_12 = pd.Series(df['close'].ewm(span=12, min_periods=12).mean())
EMA_26 = pd.Series(df['close'].ewm(span=26, min_periods=26).mean())
df['MACD'] = pd.Series(EMA_12 - EMA_26)
df['MACD_signal'] = pd.Series(df.MACD.ewm(span=9, min_periods=9).mean())

上一篇
股價預測篇-分析part4(金融類)
下一篇
股價預測篇-建模part6(金融類)
系列文
從無到有,爬蟲-分析-預測建模,把實務面常見問題逐一釐清18
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言